package com.shr.lease.common.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.shr.lease.common.exception.LeaseException;
import com.shr.lease.common.result.ResultCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Haoran Sun
 * @since 2024/06/24
 */

@Configuration
@EnableConfigurationProperties(AliyunSMSProperties.class)
@ConditionalOnProperty(prefix = "aliyun.sms",name = "endpoint")
public class AliyunSMSConfiguration {

    @Resource
    private AliyunSMSProperties aliyunSMSProperties;

    @Bean
    public Client getClient()  {
        try {
            return new Client(new Config()
                    .setAccessKeyId(aliyunSMSProperties.getAccessKeyId())
                    .setAccessKeySecret(aliyunSMSProperties.getAccessKeySecret())
                    .setEndpoint(aliyunSMSProperties.getEndpoint())
            );
        }catch (Exception e) {
            e.printStackTrace();
            throw new LeaseException(ResultCodeEnum.FAIL);
        }

    }

}
